/**
 * Created by pjn on 2017/9/20.
 */
var provinceSelect = "";//按省份查询
var citySelect = "";//按市查询
var roadSelect = "";//按道路查询
var startRoadSelect = "";//按起始道路查询
var endRoadSelect = "";//按终止道路查询
var areaSelect = "";//按区域查询
var orgSelect = "";//按主管单位查询
var orgConservationSelect = "";//按养护公司查询
var orgName = -1;
var orgConservationName = -1;
var startRoadName = -1;
var endRoadName = -1;
//按省份查询的下拉框
$.getProvinceName = function () {
    $.ajax({
        url: '../rds_highLeverLocationCount/getProvinceName',
        type: 'post',
        data: {
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    provinceSelect = "<div class='input-group input-group-sm'><select class='form-control' id='provinceChoose'> <option value='-1'>省份</option>";
                    var flag = 0;
                    for (var i = 0; i < msg.jsonData.length; i++) {
                        if (msg.jsonData[i] == "" || msg.jsonData[i] == null) {
                            if (flag == 0) {
                                provinceSelect += "<option value=''>未知省份</option> ";
                                flag = 1;
                            }
                        }
                        else
                            provinceSelect += "<option value='" + msg.jsonData[i] + "'>" + msg.jsonData[i] + "</option>";
                    }
                    provinceSelect += "</select></div>";
                }
            }
        }
    });
};

//按市查询的下拉框
$.getCityName = function (provinceName) {
    $.ajax({
        url: '../rds_highLeverLocationCount/getCityName',
        type: 'post',
        data: {
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId,
            provinceName: provinceName
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    citySelect = "<option value='-1'>市</option>";
                    var flag = 0;
                    for (var i = 0; i < msg.jsonData.length; i++) {
                        if (msg.jsonData[i] == "" || msg.jsonData[i] == null) {
                            if (flag == 0) {
                                citySelect += "<option value=''>未知市</option> ";
                                flag = 1;
                            }
                        }
                        else
                            citySelect += "<option value='" + msg.jsonData[i] + "'>" + msg.jsonData[i] + "</option>";
                    }
                    $("#cityChoose").html(citySelect);
                }
            }
        }
    });
};

//按区域查询的下拉框
$.getAllAreaName = function (provinceName, cityName) {
    $.ajax({
        url: '../rds_highLeverLocationCount/getAllAreaName',
        type: 'post',
        data: {
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId,
            provinceName: provinceName,
            cityName: cityName
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    areaSelect = "<option value='-1'>区域</option>";
                    var flag = 0;
                    for (var i = 0; i < msg.jsonData.length; i++) {
                        if (msg.jsonData[i] == "" || msg.jsonData[i] == null) {
                            if (flag == 0) {
                                areaSelect += "<option value=''>未知区域</option> ";
                                flag = 1;
                            }
                        }
                        else
                            areaSelect += "<option value='" + msg.jsonData[i] + "'>" + msg.jsonData[i] + "</option>";
                    }
                    $("#areaChoose").html(areaSelect);
                }
            }
        }
    });
};

//按道路查询的下拉框
$.getAllRoadName = function (provinceName, cityName, areaName) {
    $.ajax({
        url: '../rds_highLeverLocationCount/getAllRoadName',
        type: 'post',
        data: {
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId,
            provinceName: provinceName,
            cityName: cityName,
            areaName: areaName
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    roadSelect = "<option value='-1'>道路</option>";
                    var flag = 0;
                    for (var i = 0; i < msg.jsonData.length; i++) {
                        if (msg.jsonData[i] == null || msg.jsonData[i].length == 0) {
                            if (flag == 0) {
                                roadSelect += "<option value=''>未知道路</option>";
                                flag = 1;
                            }
                        }
                        else
                            roadSelect += "<option value='" + msg.jsonData[i] + "'>" + msg.jsonData[i] + "</option>";
                    }
                    $("#roadChoose").html(roadSelect);
                }
            }
        }
    });
};

//按起始道路查询的下拉框
$.getStartRoadName = function (provinceName, cityName, areaName, endRoadName, roadName) {
    $.ajax({
        url: '../rds_highLeverLocationCount/getStartRoadName',
        type: 'post',
        data: {
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId,
            provinceName: provinceName,
            cityName: cityName,
            areaName: areaName,
            endRoadName: endRoadName,
            roadName: roadName
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    startRoadSelect = "<option value='-1'>起/止</option>";
                    var flag = 0;
                    for (var i = 0; i < msg.jsonData.length; i++) {
                        if (msg.jsonData[i] == "" || msg.jsonData[i] == null) {
                            if (flag == 0) {
                                startRoadSelect += "<option value=''>未知</option> ";
                                flag = 1;
                            }
                        }
                        else
                            startRoadSelect += "<option value='" + msg.jsonData[i] + "'>" + msg.jsonData[i] + "</option>";
                    }
                    $("#startRoadChoose").html(startRoadSelect);
                }
            }
        }
    });
};

//按终止道路查询的下拉框
$.getEndRoadName = function (startName, provinceName, cityName, areaName, roadName) {
    $.ajax({
        url: '../rds_highLeverLocationCount/getEndRoadName',
        type: 'post',
        data: {
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId,
            startRoad: startName,
            provinceName: provinceName,
            cityName: cityName,
            areaName: areaName,
            roadName: roadName
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    endRoadSelect = "<option value='-1'>止/起</option>";
                    var flag = 0;
                    for (var i = 0; i < msg.jsonData.length; i++) {
                        if (msg.jsonData[i] == "" || msg.jsonData[i] == null) {
                            if (flag == 0) {
                                endRoadSelect += "<option value=''>未知</option> ";
                                flag = 1;
                            }
                        }
                        else
                            endRoadSelect += "<option value='" + msg.jsonData[i] + "'>" + msg.jsonData[i] + "</option>";
                    }
                    $("#endRoadChoose").html(endRoadSelect);
                }
            }
        }
    });
};

//按主管单位查询的下拉框
$.getAllOrg = function () {
    $.ajax({
        url: '../rds_count/getAllOrg',
        type: 'post',
        data: {
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    orgSelect = "<div class='input-group input-group-sm'>";
                    if (gUserLoginResult.orgType == 1 && msg.jsonData.length == 1) {
                        orgSelect += "<select class='form-control' id='orgChoose' style='display: none'> <option value='-1'>主管单位</option>";
                    }
                    else if (gUserLoginResult.orgType == 0 && msg.jsonData.length == 0) {
                        orgSelect += "<select class='form-control' id='orgChoose' style='display: none'> <option value='-1'>主管单位</option>";
                    }
                    else {
                        orgSelect += "<select class='form-control' id='orgChoose' style='width: 150px'> <option value='-1'>主管单位</option>";
                    }
                    for (var i = 0; i < msg.jsonData.length; i++) {
                        orgSelect += "<option value='" + msg.jsonData[i].orgId + "'>" + msg.jsonData[i].orgName + "</option>";
                    }
                    orgSelect += "</select></div>";
                    $("#htmlOrgSelect").html(orgSelect);
                }
            }
        }
    });
};

//按维护单位查询的下拉框
$.getConservationOrg = function (mainOrgId) {
    $.ajax({
        url: '../rds_count/getConservationOrg',
        type: 'post',
        data: {
            mainOrgId: mainOrgId,
            userId: gUserLoginResult.userId,
            orgId: gUserLoginResult.orgId
        },
        dataType: 'json',
        success: function (msg) {
            if (msg != null && typeof (msg) != "undefined") {
                if (msg.isSuccess == true) {
                    if (msg != null && typeof (msg) != "undefined") {
                        if (msg.isSuccess == true) {
                            orgConservationSelect = '';
                            if (msg.jsonData.length > 1) {
                                orgConservationSelect = "<option value='-1'>养护公司</option>";
                            }
                            if (msg.jsonData.length == 0) {
                                orgConservationSelect = "<option value='-1'>无权限</option>";
                            }
                            for (var i = 0; i < msg.jsonData.length; i++) {
                                orgConservationSelect += "<option value='" + msg.jsonData[i].orgId + "'>" + msg.jsonData[i].orgName + "</option>";
                            }
                            orgConservationSelect += "</select>";
                            $("#orgConservationChoose").html(orgConservationSelect);
                            if (gUserLoginResult.orgType == 0 && msg.jsonData.length == 1) {
                                $("#orgConservationChoose").hide();
                            }
                        }
                    }
                }
            }
        }
    });
};

$(function () {
    $.getProvinceName();
    $.getAllOrg();
    gMainDT = $("#mainDataTables").DataTable({
        ajax: {
            url: "../rds_highLeverLocationCount/getHighLever4DT",
            type: "POST",
            data: function (d) {
                d.userId = gUserLoginResult.userId;
                d.orgId = gUserLoginResult.orgId;
                d.search = $('#mainDataTables').DataTable().search(this.value);
            }
        },
        columns: [{
            data: "highLeverId"
        }, {
            data: "highLeverPhoto",
            render: function (data, type, row) {
                var img = "../../images/default_photo.png";
                if (data != null) {
                    if (data.length != 0) {
                        img = data;
                    }
                }
                return "<img src='" + img + "' style='width:50px;height:40px;cursor: pointer' " +
                    "onclick='$.showPicModal(\"" + img + "\")'> ";
            }
        }, {
            data: "highLeverArea",
            render: function (data, type, row) {
                return row.highLeverProvince + ' ' + row.highLeverCity + ' ' + row.highLeverArea;
            }
        }, {
            data: "highLeverRoadName"
        }, {
            data: "highLeverStartRoad",
            render: function (data, type, row) {
                if (data == "") {
                    return "无";
                }
                return data;
            }
        }, {
            data: "highLeverEndRoad",
            render: function (data, type, row) {
                if (data == "") {
                    return "无";
                }
                return data;
            }
        }, {
            data: "highLeverMainOrgName"
        }, {
            data: "highLeverOrgName"
        }, {
            data: "highLeverSize"
        }, {
            data: "highLeverIsComplete",
            render: function (data, type, row) {
                //信息是否齐全(0:不齐全, 1:齐全)
                if (data == 0) {
                    return "<button class='btn btn-danger btn-xs btn-flat'>不齐全</button>";
                }
                else
                    return "<button class='btn btn-success btn-xs btn-flat'>齐全</button>";
            }
        }, {
            data: "highLeverInstallTime",
            render: function (data, type, row) {
                var time = data.substring(0, 10);
                return time;
            }
        }],
        dom: "<'row'<'col-md-2'l><'#mainToolbar.col-md-10'>r>t<'row'<'col-md-6'i><'col-md-6'p>>",
        initComplete: function () {
            // 下拉工具栏按钮的权限
            $("#mainToolbar").empty();
            var strToolBar = "<div class='pull-right'><div class='btn-group'><button type='button' class='btn btn-primary btn-sm btn-flat'><i class='fa fa-cog'></i> 操作</button>";
            strToolBar += "<button type='button' class='btn btn-primary btn-sm btn-flat dropdown-toggle' data-toggle='dropdown' aria-haspopup='true' aria-expanded='false'>";
            strToolBar += "<span class='caret'></span><span class='sr-only'>切换下拉</span></button><ul class='dropdown-menu'>";
            strToolBar += "<li><button href='#' class='btn btn-primary btn-sm btn-flat btn-block' onclick='gMainDT.ajax.reload(null, false);'><i class='fa fa-refresh'></i> 刷新</button>";
            strToolBar += "</ul></div> <button type='button' class='btn btn-primary btn-sm btn-flat' style='margin-right: 5px' onclick='$.toBarChart();'><i class='fa fa-bar-chart'></i>窨井分类数量统计 </button>";
            strToolBar += provinceSelect + "&nbsp;" + "<div class='input-group input-group-sm'><select class='form-control' style='width: 90px' id='cityChoose'></select></div>" + "&nbsp;" +
                "<div class='input-group input-group-sm'><select class='form-control' style='width: 90px' id='areaChoose'></select></div>" + "&nbsp;<div class='input-group input-group-sm'>" +
                "<select class='form-control' style='width: 90px' id='roadChoose'></select>";
            strToolBar += "<span class='input-group-btn'><button class='btn btn-primary btn-flat' type='button' onclick='$(this).searchDT()'><i class='fa fa-search'></i></button></span></div>&nbsp;" +
                "<div class='input-group input-group-sm'><span class='input-group-btn'><button class='btn btn-primary btn-flat' type='button' onclick='$.showSearchModal()'><i class='glyphicon glyphicon-plus'></i></button></span></div></div></div>";
            $("#mainToolbar").append(strToolBar);
            $.getCityName(-1);
            $.getAllAreaName(-1, -1);
            $.getAllRoadName(-1, -1, -1);
            $.getStartRoadName(-1, -1, -1, -1, -1);
            $.getEndRoadName(-1, -1, -1, -1, -1);
            $.getConservationOrg(-1);
            //省
            $("#provinceChoose").change(function () {
                $.getCityName($("#provinceChoose").val());
                $.getAllAreaName($("#provinceChoose").val(), $("#cityChoose").val());
                $.getAllRoadName($("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val());
                $.getStartRoadName($("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#endRoadChoose").val(), $("#roadChoose").val());
                $.getEndRoadName($("#startRoadChoose").val(), $("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#roadChoose").val());
            });
            //市
            $("#cityChoose").change(function () {
                $.getAllAreaName($("#provinceChoose").val(), $("#cityChoose").val());
                $.getAllRoadName($("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val());
                $.getStartRoadName($("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#endRoadChoose").val(), $("#roadChoose").val());
                $.getEndRoadName($("#startRoadChoose").val(), $("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#roadChoose").val());
            });
            //区
            $("#areaChoose").change(function () {
                $.getAllRoadName($("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val());
                $.getStartRoadName($("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#endRoadChoose").val(), $("#roadChoose").val());
                $.getEndRoadName($("#startRoadChoose").val(), $("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#roadChoose").val());
            });
            //路
            $("#roadChoose").change(function () {
                $.getStartRoadName($("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#endRoadChoose").val(), $("#roadChoose").val());
                $.getEndRoadName($("#startRoadChoose").val(), $("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#roadChoose").val());
            });
        },
        processing: true,
        serverSide: true,
        ordering: false,
        select: false,
        autoFill: true,
        displayLength: 20,
        lengthMenu: [[5, 10, 15, 20, 50, 100, 150, -1], [5, 10, 15, 20, 50, 100, 150, "All"]],
        language: {
            url: "zh_cn.json"
        },
        fnDrawCallback: function () {// 序号自增回调函数
            var api = this.api();
            var startIndex = api.context[0]._iDisplayStart;
            api.column(0).nodes().each(function (cell, i) {
                cell.innerHTML = startIndex + i + 1;
            });
        }
    });

});
// 搜索框查询操作
$.fn.searchDT = function () {
    //在对表格进行查询时，如果没有选择值项进行查询，该变量的值就赋值为-1，在后台用特殊字符"#"进行拆分
    var provinceName = $("#provinceChoose").val();
    var cityName = $("#cityChoose").val();
    var areaName = $("#areaChoose").val();
    var roadName = $("#roadChoose").val();

    var searchContext = provinceName + "#" + cityName + "#" + areaName + "#" + roadName + "#" + startRoadName + "#"
        + endRoadName + "#" + orgName + "#" + orgConservationName;
    gMainDT.search(searchContext).draw();
};

//重置搜索条件
$.reStart = function () {

    $("#startRoadChoose option:first").prop("selected", 'selected');
    $.getEndRoadName($("#startRoadChoose").val(), $("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#roadChoose").val());
    $("#orgChoose option:first").prop("selected", 'selected');
    $.getConservationOrg($("#orgChoose").val());
};

//精细搜索
$.mySearch = function () {
    startRoadName = $("#startRoadChoose").val();
    endRoadName = $("#endRoadChoose").val();
    orgName = $("#orgChoose").val();
    orgConservationName = $("#orgConservationChoose").val();
    $.fn.searchDT();
};

//显示搜索模态框
$.showSearchModal = function () {
    $("#label-search").text("精细查询");
    $("#modal-search").modal({
        backdrop: 'static',
        keyboard: false,
        show: true
    });
    //起始路段
    $("#startRoadChoose").change(function () {
        $.getEndRoadName($("#startRoadChoose").val(), $("#provinceChoose").val(), $("#cityChoose").val(), $("#areaChoose").val(), $("#roadChoose").val());
    });
    //主管
    $("#orgChoose").change(function () {
        $.getConservationOrg($("#orgChoose").val());
    });
};

//跳转柱状图
$.toBarChart = function () {
    $("#workbench").load("count/highLeverStyleCount.html");
};